import os, sys
#按照种类，分别计算每个种类的合并之后的P,R，F1（比较重要）
#根据测试后标注的结果和原文进行对照，将标注的结果中的word进行补充完整，并生成label_test_entire（只包含word和pre_label）
# 和label_test_all（包含word和标注的label和pre_label）
def test_data_match(args):
    with open(args+'\\script\\label_test.txt','r',encoding="UTF-8") as f:
        text1 = f.read()

    sentences_label_test = text1.split('\n')
    # print(sentences)
    # print(sentences_label_test[0])
    # print(type(sentences_label_test))
    label_test = []
    for i in range(0, len(sentences_label_test)):
        word = sentences_label_test[i].split('\t')
        # print(word[2])
        # print(word)
        # print(len(word))
        label_test.append(word[2])
    # print(len(sentences_label_test))

    # print(type(word))
    # print(label_test)
    with open(args+'\\script\\test.txt','r',encoding="UTF-8") as f:
        text_test = f.read()

    
    with open(args+'\\script\\label_test_entire.txt', "w", encoding="UTF-8") as file:
        with open(args+'\\script\\label_test_all.txt', "w", encoding="UTF-8") as file_all:
            sentences_test = text_test.split('\n')
            k = 0
            for i in range(0, len(sentences_test)):
                word = sentences_test[i].split(' ')
                if word == ['']:
                    file.write("\n")
                    file_all.write("\n")
                else:
                    file.write(word[0] + " " + label_test[k] + '\n')
                    file_all.write(word[0] + " " + word[1] + " " + label_test[k] + '\n')
                    k = k + 1


def get_P_R_F1(args):
    def Get_TP(list1, list2):
        same_list = [line for line in list1 if line in list2]
        return len(same_list)

    def Get_Num(single_sentences, single_label, single_pre_label):
        time_list = [];
        orig_list = [];
        dest_list = [];
        oair_list = [];
        dair_list = [];
        pass_list = [];
        type_list = []
        loc_list = [];
        org_list = [];
        task_list = [];
        reg_list = [];
        call_list = []
        place_list = [];
        air_list = []

        pre_time_list = [];
        pre_orig_list = [];
        pre_dest_list = [];
        pre_oair_list = [];
        pre_dair_list = [];
        pre_pass_list = []
        pre_type_list = [];
        pre_loc_list = [];
        pre_org_list = [];
        pre_task_list = [];
        pre_reg_list = [];
        pre_call_list = []
        pre_place_list = [];
        pre_air_list = []

        time_num = 0;
        orig_num = 0;
        dest_num = 0;
        oair_num = 0;
        dair_num = 0;
        pass_num = 0;
        type_num = 0;
        loc_num = 0;
        org_num = 0;
        task_num = 0;
        reg_num = 0;
        call_num = 0
        place_num = 0;
        air_num = 0
        pre_time_num = 0;
        pre_orig_num = 0;
        pre_dest_num = 0;
        pre_oair_num = 0;
        pre_dair_num = 0;
        pre_pass_num = 0;
        pre_type_num = 0;
        pre_loc_num = 0;
        pre_org_num = 0;
        pre_task_num = 0;
        pre_reg_num = 0;
        pre_call_num = 0
        pre_place_num = 0;
        pre_air_num = 0

        single_word_list = single_sentences.split(" ")
        single_label_list = single_label.split(" ")
        single_pre_label_list = single_pre_label.split(" ")
        for i in range(0, len(single_word_list)):
            time_str = ""
            orig_str = ""
            dest_str = ""
            oair_str = ""
            dair_str = ""
            pass_str = ""
            type_str = ""
            loc_str = ""
            org_str = ""
            task_str = ""
            reg_str = ""
            call_str = ""
            place_str = ""
            air_str = ""
            if single_label_list[i] == 'B-TIME':
                time_str = time_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_label_list[j] != 'I-TIME':
                        time_list.append(time_str)
                        time_num = time_num + 1
                        break
                    elif single_label_list[j] == "I-TIME":
                        time_str = time_str + ' ' + single_word_list[j]
            elif single_label_list[i] == 'B-ORIG':
                orig_str = orig_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_label_list[j] != 'I-ORIG':
                        orig_list.append(orig_str)
                        orig_num = orig_num + 1
                        break
                    elif single_label_list[j] == "I-ORIG":
                        # time_list.append(single_word_list[j])
                        orig_str = orig_str + ' ' + single_word_list[j]
            elif single_label_list[i] == 'B-DEST':
                dest_str = dest_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_label_list[j] != 'I-DEST':
                        dest_list.append(dest_str)
                        dest_num = dest_num + 1
                        break
                    elif single_label_list[j] == "I-DEST":
                        dest_str = dest_str + ' ' + single_word_list[j]
            elif single_label_list[i] == 'B-OAIR':
                oair_str = oair_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_label_list[j] != 'I-OAIR':
                        oair_list.append(oair_str)
                        oair_num = oair_num + 1
                        break
                    elif single_label_list[j] == "I-OAIR":
                        oair_str = oair_str + ' ' + single_word_list[j]
            elif single_label_list[i] == 'B-DAIR':
                dair_str = dair_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_label_list[j] != 'I-DAIR':
                        dair_list.append(dair_str)
                        dair_num = dair_num + 1
                        break
                    elif single_label_list[j] == "I-DAIR":
                        # time_list.append(single_word_list[j])
                        dair_str = dair_str + ' ' + single_word_list[j]
            elif single_label_list[i] == 'B-PASS':
                pass_str = pass_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_label_list[j] != 'I-PASS':
                        pass_list.append(pass_str)
                        pass_num = pass_num + 1
                        break
                    elif single_label_list[j] == "I-PASS":
                        pass_str = pass_str + ' ' + single_word_list[j]
            elif single_label_list[i] == 'B-TYPE':
                type_str = type_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_label_list[j] != 'I-TYPE':
                        type_list.append(type_str)
                        type_num = type_num + 1
                        break
                    elif single_label_list[j] == "I-TYPE":
                        type_str = type_str + ' ' + single_word_list[j]
            elif single_label_list[i] == 'B-LOC':
                loc_str = loc_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_label_list[j] != 'I-LOC':
                        loc_list.append(loc_str)
                        loc_num = loc_num + 1
                        break
                    elif single_label_list[j] == "I-LOC":
                        loc_str = loc_str + ' ' + single_word_list[j]
            elif single_label_list[i] == 'B-ORG':
                org_str = org_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_label_list[j] != 'I-ORG':
                        org_list.append(org_str)
                        org_num = org_num + 1
                        break
                    elif single_label_list[j] == "I-ORG":
                        org_str = org_str + ' ' + single_word_list[j]
            elif single_label_list[i] == 'B-TASK':
                task_str = task_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_label_list[j] != 'I-TASK':
                        task_list.append(task_str)
                        task_num = task_num + 1
                        break
                    elif single_label_list[j] == "I-TASK":
                        task_str = task_str + ' ' + single_word_list[j]
            elif single_label_list[i] == 'B-REG':
                reg_str = reg_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_label_list[j] != 'I-REG':
                        reg_list.append(reg_str)
                        reg_num = reg_num + 1
                        break
                    elif single_label_list[j] == "I-REG":
                        reg_str = reg_str + ' ' + single_word_list[j]
            elif single_label_list[i] == 'B-CALL':
                call_str = call_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_label_list[j] != 'I-CALL':
                        call_list.append(call_str)
                        call_num = call_num + 1
                        break
                    elif single_label_list[j] == "I-CALL":
                        call_str = call_str + ' ' + single_word_list[j]
        for i in range(0, len(single_word_list)):
            pre_time_str = ""
            pre_orig_str = ""
            pre_dest_str = ""
            pre_oair_str = ""
            pre_dair_str = ""
            pre_pass_str = ""
            pre_type_str = ""
            pre_loc_str = ""
            pre_org_str = ""
            pre_task_str = ""
            pre_reg_str = ""
            pre_call_str = ""
            if single_pre_label_list[i] == 'B-TIME':
                pre_time_str = pre_time_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_pre_label_list[j] != 'I-TIME':
                        pre_time_list.append(pre_time_str)
                        pre_time_num = pre_time_num + 1
                        break
                    elif single_pre_label_list[j] == "I-TIME":
                        pre_time_str = pre_time_str + ' ' + single_word_list[j]
            elif single_pre_label_list[i] == 'B-ORIG':
                pre_orig_str = pre_orig_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_pre_label_list[j] != 'I-ORIG':
                        pre_orig_list.append(pre_orig_str)
                        pre_orig_num = pre_orig_num + 1
                        break
                    elif single_pre_label_list[j] == "I-ORIG":
                        pre_orig_str = pre_orig_str + ' ' + single_word_list[j]
            elif single_pre_label_list[i] == 'B-DEST':
                pre_dest_str = pre_dest_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_pre_label_list[j] != 'I-DEST':
                        pre_dest_list.append(pre_dest_str)
                        pre_dest_num = pre_dest_num + 1
                        break
                    elif single_pre_label_list[j] == "I-DEST":
                        pre_dest_str = pre_dest_str + ' ' + single_word_list[j]
            elif single_pre_label_list[i] == 'B-OAIR':
                pre_oair_str = pre_oair_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_pre_label_list[j] != 'I-OAIR':
                        pre_oair_list.append(pre_oair_str)
                        pre_oair_num = pre_oair_num + 1
                        break
                    elif single_pre_label_list[j] == "I-OAIR":
                        pre_oair_str = pre_oair_str + ' ' + single_word_list[j]
            elif single_pre_label_list[i] == 'B-DAIR':
                pre_dair_str = pre_dair_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_pre_label_list[j] != 'I-DAIR':
                        pre_dair_list.append(pre_dair_str)
                        pre_dair_num = pre_dair_num + 1
                        break
                    elif single_pre_label_list[j] == "I-DAIR":
                        pre_dair_str = pre_dair_str + ' ' + single_word_list[j]
            elif single_pre_label_list[i] == 'B-PASS':
                pre_pass_str = pre_pass_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_pre_label_list[j] != 'I-PASS':
                        pre_pass_list.append(pre_pass_str)
                        pre_pass_num = pre_pass_num + 1
                        break
                    elif single_pre_label_list[j] == "I-PASS":
                        pre_pass_str = pre_pass_str + ' ' + single_word_list[j]
            elif single_pre_label_list[i] == 'B-TYPE':
                pre_type_str = pre_type_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_pre_label_list[j] != 'I-TYPE':
                        pre_type_list.append(pre_type_str)
                        pre_type_num = pre_type_num + 1
                        break
                    elif single_pre_label_list[j] == "I-TYPE":
                        pre_type_str = pre_type_str + ' ' + single_word_list[j]
            elif single_pre_label_list[i] == 'B-LOC':
                pre_loc_str = pre_loc_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_pre_label_list[j] != 'I-LOC':
                        pre_loc_list.append(pre_loc_str)
                        pre_loc_num = pre_loc_num + 1
                        break
                    elif single_pre_label_list[j] == "I-LOC":
                        pre_loc_str = pre_loc_str + ' ' + single_word_list[j]
            elif single_pre_label_list[i] == 'B-ORG':
                pre_org_str = pre_org_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_pre_label_list[j] != 'I-ORG':
                        pre_org_list.append(pre_org_str)
                        pre_org_num = pre_org_num + 1
                        break
                    elif single_pre_label_list[j] == "I-ORG":
                        pre_org_str = pre_org_str + ' ' + single_word_list[j]
            elif single_pre_label_list[i] == 'B-TASK':
                pre_task_str = pre_task_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_pre_label_list[j] != 'I-TASK':
                        pre_task_list.append(pre_task_str)
                        pre_task_num = pre_task_num + 1
                        break
                    elif single_pre_label_list[j] == "I-TASK":
                        pre_task_str = pre_task_str + ' ' + single_word_list[j]
            elif single_pre_label_list[i] == 'B-REG':
                pre_reg_str = pre_reg_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_pre_label_list[j] != 'I-REG':
                        pre_reg_list.append(pre_reg_str)
                        pre_reg_num = pre_reg_num + 1
                        break
                    elif single_pre_label_list[j] == "I-REG":
                        pre_reg_str = pre_reg_str + ' ' + single_word_list[j]
            elif single_pre_label_list[i] == 'B-CALL':
                pre_call_str = pre_call_str + single_word_list[i]
                for j in range(i + 1, len(single_word_list)):
                    if single_pre_label_list[j] != 'I-CALL':
                        pre_call_list.append(pre_call_str)
                        pre_call_num = pre_call_num + 1
                        break
                    elif single_pre_label_list[j] == "I-CALL":
                        pre_call_str = pre_call_str + ' ' + single_word_list[j]

        time_tp = Get_TP(time_list, pre_time_list)
        orig_tp = Get_TP(orig_list, pre_orig_list)
        dest_tp = Get_TP(dest_list, pre_dest_list)
        oair_tp = Get_TP(oair_list, pre_oair_list)
        dair_tp = Get_TP(dair_list, pre_dair_list)
        pass_tp = Get_TP(pass_list, pre_pass_list)
        type_tp = Get_TP(type_list, pre_type_list)
        loc_tp = Get_TP(loc_list, pre_loc_list)
        org_tp = Get_TP(org_list, pre_org_list)
        task_tp = Get_TP(task_list, pre_task_list)
        reg_tp = Get_TP(reg_list, pre_reg_list)
        call_tp = Get_TP(call_list, pre_call_list)

        return time_num, pre_time_num, time_tp, orig_num, pre_orig_num, orig_tp, dest_num, pre_dest_num, dest_tp, oair_num, pre_oair_num, oair_tp, dair_num, pre_dair_num, dair_tp, pass_num, pre_pass_num, pass_tp, type_num, pre_type_num, type_tp, loc_num, pre_loc_num, loc_tp, org_num, pre_org_num, org_tp, task_num, pre_task_num, task_tp, reg_num, pre_reg_num, reg_tp, call_num, pre_call_num, call_tp

    def Output(line, num, pre_num, Tp):

        P = Tp / float(pre_num + 1e-12)
        R = Tp / float(num + 1e-12)
        F1 = 2 * R * P / (R + P + 1e-12)
        string=line + "\t"+"P: " + str(P) + "  R: " + str(R) + "  F1: " + str(F1)
        # print(string)
        return string
    test_data_match(args)
    with open(args+'\\script\\label_test_all.txt','r',encoding="UTF-8") as f:
        text= f.read()

    sentences = text.split("\n")
    # print(sentences)
    word_list = []
    label_list = []
    pre_label_list = []
    for i in range(0, len(sentences)):
        line = sentences[i].split(" ")
        # print(line)
        word_list.append(line[0])
        if line == ['']:
            label_list.append(line[-1])
        else:
            label_list.append(line[1])
        pre_label_list.append(line[-1])

    sentences_str = ' '.join(word_list)
    label_str = ' '.join(label_list)
    pre_label_str = ' '.join(pre_label_list)
    sentences_list = sentences_str.split('  ')

    sentences_label_list = label_str.split('  ')
    sentences_pre_label_list = pre_label_str.split('  ')
    Time_num = 0;
    Pre_time_num = 0;
    Time_Tp = 0
    Orig_num = 0;
    Pre_orig_num = 0;
    Orig_Tp = 0
    Dest_num = 0;
    Pre_dest_num = 0;
    Dest_Tp = 0
    Oair_num = 0;
    Pre_oair_num = 0;
    Oair_Tp = 0
    Dair_num = 0;
    Pre_dair_num = 0;
    Dair_Tp = 0
    Pass_num = 0;
    Pre_pass_num = 0;
    Pass_Tp = 0
    Type_num = 0;
    Pre_type_num = 0;
    Type_Tp = 0
    Loc_num = 0;
    Pre_loc_num = 0;
    Loc_Tp = 0
    Org_num = 0;
    Pre_org_num = 0;
    Org_Tp = 0
    Task_num = 0;
    Pre_task_num = 0;
    Task_Tp = 0
    Reg_num = 0;
    Pre_reg_num = 0;
    Reg_Tp = 0
    Call_num = 0;
    Pre_call_num = 0;
    Call_Tp = 0


    for i in range(0, len(sentences_list)):
        time_num, pre_time_num, time_tp, orig_num, pre_orig_num, orig_tp, dest_num, pre_dest_num, dest_tp, oair_num, pre_oair_num, oair_tp, dair_num, pre_dair_num, dair_tp, pass_num, pre_pass_num, pass_tp, type_num, pre_type_num, type_tp, loc_num, pre_loc_num, loc_tp, org_num, pre_org_num, org_tp, task_num, pre_task_num, task_tp, reg_num, pre_reg_num, reg_tp, call_num, pre_call_num, call_tp = Get_Num(
            sentences_list[i], sentences_label_list[i], sentences_pre_label_list[i])

        Time_num = Time_num + time_num
        Pre_time_num = Pre_time_num + pre_time_num
        Time_Tp = Time_Tp + time_tp

        Orig_num = Orig_num + orig_num
        Pre_orig_num = Pre_orig_num + pre_orig_num
        Orig_Tp = Orig_Tp + orig_tp

        Dest_num = Dest_num + dest_num
        Pre_dest_num = Pre_dest_num + pre_dest_num
        Dest_Tp = Dest_Tp + dest_tp

        Oair_num = Oair_num + oair_num
        Pre_oair_num = Pre_oair_num + pre_oair_num
        Oair_Tp = Oair_Tp + oair_tp

        Dair_num = Dair_num + dair_num
        Pre_dair_num = Pre_dair_num + pre_dair_num
        Dair_Tp = Dair_Tp + dair_tp

        Pass_num = Pass_num + pass_num
        Pre_pass_num = Pre_pass_num + pre_pass_num
        Pass_Tp = Pass_Tp + pass_tp

        Type_num = Type_num + type_num
        Pre_type_num = Pre_type_num + pre_type_num
        Type_Tp = Type_Tp + type_tp

        Loc_num = Loc_num + loc_num
        Pre_loc_num = Pre_loc_num + pre_loc_num
        Loc_Tp = Loc_Tp + loc_tp

        Org_num = Org_num + org_num
        Pre_org_num = Pre_org_num + pre_org_num
        Org_Tp = Org_Tp + org_tp

        Task_num = Task_num + task_num
        Pre_task_num = Pre_task_num + pre_task_num
        Task_Tp = Task_Tp + task_tp

        Reg_num = Reg_num + reg_num
        Pre_reg_num = Pre_reg_num + pre_reg_num
        Reg_Tp = Reg_Tp + reg_tp

        Call_num = Call_num + call_num
        Pre_call_num = Pre_call_num + pre_call_num
        Call_Tp = Call_Tp + call_tp


    string1=Output("时间推测:",Time_num ,Pre_time_num ,Time_Tp )
    string2=Output("起飞地点（orig）:",Orig_num ,Pre_orig_num ,Orig_Tp )
    string3=Output("降落地点（dest）:",Dest_num ,Pre_dest_num ,Dest_Tp )
    string4=Output("起飞机场（oair）:",Oair_num ,Pre_oair_num ,Oair_Tp )
    string5=Output("降落机场（dair）:",Dair_num ,Pre_dair_num ,Dair_Tp )
    string6=Output("经过地点推测:",Pass_num ,Pre_pass_num ,Pass_Tp )
    string7=Output("机型推测：",Type_num ,Pre_type_num ,Type_Tp )
    string8=Output("国家（地名）（loc）:",Loc_num ,Pre_loc_num ,Loc_Tp )
    string9=Output("组织机构推测：",Org_num ,Pre_org_num ,Org_Tp )
    string10=Output("任务（task）:",Task_num ,Pre_task_num ,Task_Tp )
    string11=Output("飞机注册号（reg）:",Reg_num ,Pre_reg_num ,Reg_Tp )
    string12=Output("呼号推测：",Call_num ,Pre_call_num ,Call_Tp )

    string_display=string1+"\n"+string12
    
    return string_display

if __name__=='__main__':
    args=sys.argv[1]
    try:
        string=get_P_R_F1(args)
        print(string)
    except Exception as e:
        print(e)

